package com.lixin.smartclassroom.ui.home.tab1.activity

import android.graphics.Color
import android.view.View
import android.widget.TextView
import com.drake.brv.utils.bindingAdapter
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.courselist
import com.lixin.smartclassroom.api.delsearchlog
import com.lixin.smartclassroom.api.searchcourselist
import com.lixin.smartclassroom.api.searchloglist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.ActivitySearchBinding
import com.lixin.smartclassroom.ui.home.adapter.excitingMoment
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

/**
 * Description 搜索
 * Author: lwc
 * Date:2023/12/9 13:35
 */
class SearchAct : BaseActivity<ActivitySearchBinding>() {
    override fun getViewBinding() = ActivitySearchBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "搜索"
    private lateinit var listBean: ArrayList<DataListBean>
    private lateinit var listBeanSearchResult: ArrayList<DataListBean>


    override fun iniView() {
        listBean = ArrayList()

        listBeanSearchResult = ArrayList()
        binding.rcvSearchResult.excitingMoment().apply {
            R.id.rl.onClick {
                start(CourseDetailAct::class.java) {
                    putExtra("courseid", (models?.get(modelPosition) as DataListBean).id)
                }
            }
        }.models = listBeanSearchResult

        binding.tagFlowLayout.adapter = object : TagAdapter<DataListBean>(listBean) {
            override fun getView(parent: FlowLayout?, position: Int, t: DataListBean?): View {
                val tv = View.inflate(this@SearchAct, R.layout.search_flow_textview, null)
                val textView = tv.findViewById<TextView>(R.id.tv_name)
                textView.setRoundRectBg(Color.parseColor("#FAF9F9"), dp2px(14f).toInt())
                if (t != null) {
                    textView.text = t.name
                }
                textView.onClick {
                    scopeNetLife {
                        binding.etSearch.setText(textView.text.toString())
                        listBeanSearchResult.clear()
                        listBeanSearchResult.addAll(
                            searchcourselist(
                                textView.text.toString(),
                                "2",
                                1,
                                1000
                            )!!
                        )
                        binding.rcvSearchResult.visible()
                        binding.llSearchHistory.gone()
                        binding.rcvSearchResult.bindingAdapter.notifyDataSetChanged()
                    }
                }

                return tv
            }
        }
        getSearchHistoryData()

    }

    private fun getSearchHistoryData() {
        scopeNetLife {
            listBean.addAll(searchloglist()!!)
            binding.tagFlowLayout.adapter.notifyDataChanged()
        }
    }

    override fun onClick() {
        binding.apply {
            btnDelete.onClick {
                scopeNetLife {
                    val baseBean = delsearchlog("2")
                    listBean.clear()
                    binding.tagFlowLayout.adapter.notifyDataChanged()
                }
            }

            tvSearch.onClick {
                if (etSearch.text().isEmpty()) {
                    toast("请输入搜索内容")
                    return@onClick
                } else {
                    scopeNetLife {
                        listBeanSearchResult.clear()
                        listBeanSearchResult.addAll(
                            searchcourselist(
                                etSearch.text(),
                                "2",
                                1,
                                1000
                            )!!
                        )
                        rcvSearchResult.visible()
                        llSearchHistory.gone()
                        binding.rcvSearchResult.bindingAdapter.notifyDataSetChanged()
                    }
                }
            }
        }
    }


}